using Castle.ActiveRecord;
using demoapp.Models;

namespace demoapp.Controllers
{
	public class HomeController : BaseController
	{
		public override void SetEngineContext(Castle.MonoRail.Framework.IEngineContext engineContext)
		{
			base.SetEngineContext(engineContext);
			engineContext.CurrentControllerContext.PropertyBag["debuggingActive"] = Crumbs.Profiler.Enabled;
		}

		public void Index() {}

		public void ResetDb()
		{
			ActiveRecordStarter.CreateSchema();

			using (TransactionScope tx = new TransactionScope())
			{
				Post p = new Post("First post", "blah, blah, ...");
				p.AddComment("commenter1", "comment 1");
				p.AddComment("commenter2", "comment 2");
				p.Create();

				p = new Post("Second post", "blah, blah, blah ...");
				p.AddComment("commenter1", "comment 1");
				p.AddComment("commenter2", "comment 2");
				p.AddComment("commenter1", "comment 3");
				p.Create();

				p = new Post("Third post", "more blah, blah, vlah, ...");
				p.AddComment("commenter2", "comment 1");
				p.Create();
			}
				
			Flash["success"] = "Database successfully initialized";
			RedirectToAction("Index");
		}

		public void Posts()
		{
			Post[] posts = Post.FindAll();
			PropertyBag["posts"] = posts;
		}

		public void ToggleDebugWindow()
		{
			Crumbs.Profiler.Enabled = !Crumbs.Profiler.Enabled;
			RedirectToAction("Index");
		}
	}
}
